/*
 * @Author: ziqi jhzq12345678
 * @Date: 2025-01-21 09:12:39
 * @LastEditors: ziqi jhzq12345678
 * @LastEditTime: 2025-01-21 10:46:06
 * @FilePath: /pornhub_app/lib/views/merchant/merchant_page.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/no_more/no_more.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/merchat/merchat_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/merchant/merchat_top.dart';

class MerchantPage extends StatefulWidget {
  const MerchantPage({super.key});

  @override
  State<StatefulWidget> createState() => _MerchantPage();
}

class _MerchantPage extends State<MerchantPage> {
  final _pageSize = 20;

  final PagingController<int, MerchatModel> _pagingController =
      PagingController(firstPageKey: 1);

  Future _initListData(int pageKey) async {
    final resp = await httpInstance.get<MerchatModel>(
        url: 'info/activity/merchantList',
        queryMap: {
          'pageSize': _pageSize,
          'page': pageKey,
        },
        complete: MerchatModel.fromJson);
    if (resp == null) {
      _pagingController.appendLastPage([]);
    }
    final isLastPage = resp.length < _pageSize;
    if (isLastPage) {
      _pagingController.appendLastPage(resp);
    } else {
      final nextPageKey = pageKey + 1;
      _pagingController.appendPage(resp, nextPageKey as int?);
    }
  }

  @override
  void initState() {
    _pagingController.addPageRequestListener((pageKey) {
      _initListData(pageKey);
    });

    super.initState();
  }

  Widget buildItem(MerchatModel model) {
    return Container(
      margin: EdgeInsets.only(top: 15.w, left: 14.w, right: 14.w),
      height: 136.w,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.w),
          color: COLOR.hexColor('#181818')),
      child: Row(
        children: [
          Container(
            width: 120.w,
            height: 120.w,
            margin: EdgeInsets.only(left: 8.w, right: 8.w),
            clipBehavior: Clip.hardEdge,
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(4.w)),
            child: ImageView(src: model.coverImg ?? ''),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 8.w,
              ),
              model.tags != null
                  ? Wrap(
                      spacing: 8.w,
                      children: model.tags!.map((v) {
                        return Container(
                          padding: EdgeInsets.symmetric(
                              horizontal: 11.w, vertical: 2.w),
                          decoration: BoxDecoration(
                              color: COLOR.hexColor('#323232'),
                              borderRadius: BorderRadius.circular(10.w)),
                          child: Text(v),
                        );
                      }).toList(),
                    )
                  : const SizedBox.shrink(),
              SizedBox(
                height: 14.w,
              ),
              Text(
                '${model.merchantName}',
                style: TextStyle(fontSize: 14.w, color: Colors.white),
              ),
              SizedBox(
                height: 22.w,
              ),
              Row(
                children: [
                  Image.asset(
                    AppImagePath.activity_activity_location,
                    width: 12.w,
                  ),
                  Container(
                    width: 190.w,
                    margin: EdgeInsets.only(left: 5.w),
                    child: Text(
                      '${model.location}',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  )
                ],
              )
            ],
          )
        ],
      ),
    ).onTap(() {
      // Get.toBloggerDetail(userId: model.merchantId!);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('商家'),
      ),
      body: CustomScrollView(
        slivers: [
          const MerchatTop().sliver,
          PagedSliverList(
              pagingController: _pagingController,
              builderDelegate: PagedChildBuilderDelegate<MerchatModel>(
                  noMoreItemsIndicatorBuilder: (_) {
                return const NoMore();
              }, noItemsFoundIndicatorBuilder: (_) {
                return const NoData();
              }, itemBuilder: (context, value, index) {
                return buildItem(value);
              }))
        ],
      ),
    );
  }
}
